package com.cy.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.javassist.expr.NewArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cy.entity.JsonResult;
import com.cy.entity.Meetingroom;
import com.cy.entity.Order_list;
import com.cy.entity.Order_listExample;
import com.cy.mapper.MeetingroomMapper;
import com.cy.mapper.Order_listMapper;

@Service
public class Order_listService {
	@Autowired
	private Order_listMapper mapper;
	
	@Autowired
	private MeetingroomMapper meetingroomMapper;
	
	public List<Order_list> findAllOrder_list() {
		return mapper.selectByExample(null);
	}
	
	public Order_list findOrder_listById(Integer id) {
		return mapper.selectByPrimaryKey(id);
	}
	
	
	public List<Order_list> findOrder_listByMid(Integer mid) {
		Order_listExample oe = new Order_listExample();
		oe.createCriteria().andMidEqualTo(mid);
		return mapper.selectByExample(oe);
	}
	
//	public List<Order_list> findBusyOrder_listByMid(Integer mid) {
//		Order_listExample oe = new Order_listExample();
//		oe.createCriteria().andMidEqualTo(mid)
//		return mapper.selectByExample(oe);
//	}
	
	public List<Order_list> findOrder_listByUserId(Integer userId) {
		Order_listExample oe = new Order_listExample();
		oe.createCriteria().andUseridEqualTo(userId);
		return mapper.selectByExample(oe);
	}
	public int judgeTime(List<Order_list> list, Long start, Long end) {
		if (list.size()==0) {
			return 0;
		}
		
		for (Order_list order_list : list) {
			if(order_list.getStatus() == -1 || order_list.getStatus() == 3) {
				continue;
			}else {
				Date sDate = order_list.getStarttime();
				Date eDate = order_list.getEndtime();
				Long s1 = sDate.getTime();
				Long s2 = eDate.getTime();
				System.out.println(order_list.getOrderid()+"==============="+list.size());
				System.out.println(start+" "+end);
				System.out.println(s1+" "+s2);
				
				if(!(start<= s1 && end <=s1 || start>=s2) ) {
					System.out.println(1);
					return 1;
				}	
			}
						
		}
		
		
		return 0;
	}
	
	public void addOrder(Order_list ol) {
		mapper.insert(ol);
	}
	
	//未支付订单（5分钟）300秒
		public void orderReFresh1() {
			Order_listExample oe = new Order_listExample();
			oe.createCriteria().andStatusEqualTo(0);
			List<Order_list> list = mapper.selectByExample(oe);
			
			for (Order_list order_list : list) {
				System.out.println(order_list.getOrdertime()+"=============="+order_list.getOrdertime().getTime());
				System.out.println(new Date()+"================"+new Date().getTime());
				Long date = new Date().getTime();
				if(date - order_list.getOrdertime().getTime()>30000) {
					order_list.setStatus(-1);
					mapper.updateByPrimaryKey(order_list);
				}
			}
		}
		//待处理未接受订单（一小时）3600秒
		public void orderReFresh2() {
			Order_listExample oe = new Order_listExample();
			oe.createCriteria().andStatusEqualTo(1);
			List<Order_list> list = mapper.selectByExample(oe);
			Long date = new Date().getTime();
			for (Order_list order_list : list) {
				if(date - order_list.getOrdertime().getTime()>360000L) {
					order_list.setStatus(2);
					mapper.updateByPrimaryKey(order_list);
				}
			}
		}
		//预约中订单
			public void orderReFresh3() {
				Order_listExample oe = new Order_listExample();
				oe.createCriteria().andStatusEqualTo(2);
				List<Order_list> list = mapper.selectByExample(oe);
				Long date = new Date().getTime();
				for (Order_list order_list : list) {
					if(date >= order_list.getEndtime().getTime()) {
						order_list.setStatus(3);
						mapper.updateByPrimaryKey(order_list);
				}
			}
		}
	//查询用户对应订单
	public List<Order_list> findUserOrder(int userId){
		Order_listExample oe = new Order_listExample();
		oe.createCriteria().andUseridEqualTo(userId);
		return mapper.selectByExample(oe);
	}
	
	//通过状态查询用户对应订单
	public List<Order_list> findUserOrderByStatus(int userId, int status){
		Order_listExample oe = new Order_listExample();
		oe.createCriteria().andUseridEqualTo(userId).andStatusEqualTo(status);
		return mapper.selectByExample(oe);
	}
	
	//修改订单
	public void updateOrder(Order_list order) {
		mapper.updateByPrimaryKey(order);
		
	}
	
	public List<Order_list> findOrderByState(Integer state) {
		Order_listExample oe = new Order_listExample();
		oe.createCriteria().andStatusEqualTo(state);
		return mapper.selectByExample(oe);
	}
	

}
